package com.silknet.images;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Set;
import java.util.TreeMap;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Object_type implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8271114386133004480L;

	private int objt_id;
	private String type_name;
	private int parent_id;
	private String image_name;

	private Object_type parent;

	private ArrayList<Object_type> children;

	public int getObjt_id() {
		return objt_id;
	}

	public void setObjt_id(int objt_id) {
		this.objt_id = objt_id;
	}

	public String getType_name() {
		return type_name;
	}

	public void setType_name(String type_name) {
		this.type_name = type_name;
	}

	public int getParent_id() {
		return parent_id;
	}

	public void setParent_id(int parent_id) {
		this.parent_id = parent_id;
	}

	public String getImage_name() {
		return image_name;
	}

	public void setImage_name(String image_name) {
		this.image_name = image_name;
	}

	public Object_type getParent() {
		return parent;
	}

	public void setParent(Object_type parent) {
		this.parent = parent;
	}

	public ArrayList<Object_type> getChildren() {
		return children;
	}

	public void setChildren(ArrayList<Object_type> children) {
		this.children = children;
	}

	public void setParentAndChilds(TreeMap<Integer, Object_type> value) {
		parent = value.get(parent_id);
		Set<Integer> keys = value.keySet();
		for (Integer key : keys) {
			Object_type r = value.get(key);
			if (r.parent_id == objt_id) {
				if (children == null)
					children = new ArrayList<Object_type>();
				children.add(r);
			}
		}
	}

	public static String convertUTF(String value) {
		if (value == null)
			return "";
		char[] chars = value.toCharArray();
		value = "";
		for (char c : chars) {
			int charcode = c;
			if (charcode > 256)
				value += "&#" + charcode;
			else
				value += c;
		}
		return value;
	}

	public void generateMenu(StringBuffer sb, String tab) {
		boolean haschildren = !(children == null || children.isEmpty());
		sb.append("\n" + tab + "<li>\n" + tab + "\t<input id=\"" + objt_id
				+ "\" type=\"checkbox\"");
		if (!haschildren)
			sb.append(" type_id=\"" + getObjt_id() + "\"");
		// String img="";
		// if(image_name!=null && !image_name.trim().isEmpty())
		// img="<img src=\""
		// + ImageStore.GENERATED_DIR + "/" + objt_id + "."
		// + ImageStore.EXT + "\" height=\"16\" width=\"16\" />";
		sb.append(">\n" + tab + "\t<label>" +
		// img+
				convertUTF(type_name) + "</label>");
		if (haschildren) {
			sb.append("\n" + tab + "\t<ul>");
			for (Object_type child : children) {
				child.generateMenu(sb, tab + "\t");
			}
			sb.append("\n" + tab + "\t</ul>");
		}
		sb.append("\n" + tab + "</li>");
	}

	public JSONObject generateComboMenu() {
		boolean haschildren = !(children == null || children.isEmpty());
		JSONObject jsn = new JSONObject();
		jsn.put("id", getObjt_id());
		jsn.put("text", (type_name));
		jsn.put("value", getObjt_id());

		if (haschildren) {
			JSONArray jsnchilddren = new JSONArray();
			for (Object_type child : children) {
				jsnchilddren.add(child.generateComboMenu());
			}
			jsn.put("children", jsnchilddren);
		}
		return jsn;
	}

	
	
}
